package com.leyou.trade.web;

import com.leyou.trade.dto.OrderDTO;
import com.leyou.trade.entity.Order;
import com.leyou.trade.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping
    public ResponseEntity<Long> addOrder(@RequestBody OrderDTO orderVO){
        return ResponseEntity.ok(orderService.addOrder(orderVO));
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<Order> getOrder(@PathVariable("orderId")Long orderId){
        return ResponseEntity.ok(orderService.getById(orderId));
    }

    @GetMapping("/status/{id}")
    public ResponseEntity<Integer> getOrderStatus(@PathVariable("id")Long orderId){
        return ResponseEntity.ok(this.orderService.getById(orderId).getStatus());
    }

    @GetMapping("/close")
    public ResponseEntity<Void> closeOrder(){
        this.orderService.closeOrder();
        return ResponseEntity.noContent().build();
    }
}
